#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "load.h"
#include "elf.h"
#include "endian.h"


int main(int argc, char* argv[]){
	
	if(argc != 3){
		printf ("Erreur d'utilisation du programme !");
		return 0;
	}

  char name[50];
	char c;
	int i=0,j=0,trouve=0;
	FILE* f;
	f = fopen(argv[1],"r");

   Elf32_Off	offset;		/* Section file offset */
   Elf32_Word size;


	Elf32_Ehdr header = load_header(f);
	Elf32_Shdr* sheader = load_section_headers(f);

	while(i<header.e_shnum && !trouve){
		if(atoi(argv[2])==i){     // ATTENTION ATOI(argv[]) donne 0 si c'est une chaine de caractere !
			offset = sheader[i].sh_offset; 
			size = sheader[i].sh_size ;
//			trouve = 0; 
		}

		if(!trouve) {
		  fseek (f ,sheader[header.e_shstrndx].sh_offset + sheader[i].sh_name, SEEK_SET);
		  c = (char)fgetc(f);
		  while (c != '\0') {
		          name[j] = c;
		          c = (char)fgetc(f);
		          j++;
		  }
		  name[j] = c;
			j=0;

			int egal=1;

			if(strlen(argv[2])<=50){
				while(egal && j<strlen(argv[2])){
					if(argv[2][j]!=name[j]){
						egal = 0;
					}
					j++;
				}
				if(egal){
					offset = sheader[i].sh_offset; 
					size = sheader[i].sh_size ;
					trouve = 1;
				}		
			}
		}
		j=0;
		i++;
	}

	j=0;
	fseek (f ,offset, SEEK_SET);
	for(i=0;i<(size/4);i++){
		fread(&j ,1 ,4 ,f );
		printf("%08x ",be32toh(j));
	}
	printf("\n");

	return 0;
}

